//
//  LoginViewController.swift
//  LongTermInsurance
//
//  Created by zolobdz on 2018/11/18.
//  Copyright © 2018年 HeChao. All rights reserved.
//

import UIKit
import ObjectMapper
import IQKeyboardManagerSwift

class LoginViewController: BaseViewController {
    @IBOutlet weak var topImageView: UIImageView!
    
    @IBOutlet weak var idTextField: UITextField!
    
    @IBOutlet weak var passwordTextField: UITextField!
    
    @IBOutlet weak var loginButton: UIButton!
    
    @IBOutlet weak var signupButton: UIButton!
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "登录"
        
        idTextField.delegate = self
        passwordTextField.delegate = self
        
        loginButton.addTarget(self, action: #selector(loginButtonAction), for: .touchUpInside)
        signupButton.addTarget(self, action: #selector(signupButtonAction), for: .touchUpInside)
        
        #if DEBUG

        idTextField.text = "320522192310231015"
        passwordTextField.text = "000000"

//            idTextField.text = "320504196001170517"
//            passwordTextField.text = "170517"
        #endif
        
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        if let idString = UserDefaults.standard.object(forKey: CDUserDefaultKey.loginID) as? String, !idString.isEmpty,let pwdString = UserDefaults.standard.object(forKey: CDUserDefaultKey.loginPassword) as? String, !pwdString.isEmpty {
            idTextField.text = idString
            passwordTextField.text = pwdString
        }
    }

    @objc func loginButtonAction() {
        guard let idString = idTextField.text,idString.count > 0 else {
            return
        }
        
        guard let pwdString = passwordTextField.text,pwdString.count > 0 else {
            return
        }
        
//        let values = ["IDNum":"320504196001170517","Password":"170517"].convertToJsonString()//"{\"IDNum\": \"320503193409092022\", \"Password\": \"092022\"}";
        let values = ["IDNum": idString,"Password": pwdString].convertToJsonString()
        let p0 = "<CodeName>"+"loginapp"+"</CodeName>"
        let p1 = "<json>"+values+"</json>"
        
        let paramString = p0 + p1
        
        NetworkingHandle.oldfetchNetworkData(method: "ExeHNService", paramString: paramString, success: { (dict) in
            if let code = dict["Code"] as? Int,code == 0 {
                if let array = dict["Message"] as? [Any],let data = array.first as? Dictionary<String, Any> {
                    
                    UserDefaults.standard.set(idString, forKey: CDUserDefaultKey.loginID)
                    UserDefaults.standard.set(pwdString, forKey: CDUserDefaultKey.loginPassword)
                    
                    if let user = Mapper<User>().map(JSON: data) {
                        MyselfSingleton.shared.updateMyself(user)
                        CDLocationService.shared.userUploadLocation()
                        self.navigationController?.dismiss(animated: true, completion: nil)
                    }
                }
                return
            }
            if let code = dict["Code"] as? Int,code == 1 {
                if let msgString = dict["Message"] as? String {
                    CDUtility.showMSG(message: msgString)
                }
            }
        }) {
            print("")
        }
        
    }
    
    @objc func signupButtonAction() {
        navigationController?.pushViewController(SignupViewController(), animated: true)
    }
    @IBAction func forgetPasswordAction(_ sender: Any) {
        navigationController?.pushViewController(ForgetPasswordViewController(), animated: true)
    }
    
}

extension LoginViewController: UITextFieldDelegate{
    
    
    
}
